package com.dengyuanke.wx.handler;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.WxSession;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Random;
import java.util.regex.Pattern;

/**
 * Created by dengyuanke on 2017/10/30.
 */
@Component
public class GuessNumberHandler extends AbstractHandler{

    private Random random = new Random();

    private Pattern pattern = Pattern.compile("\\d+");

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMpXmlMessage, Map<String, Object> map, WxMpService wxMpService, WxSessionManager wxSessionManager) throws WxErrorException {
        try {
            WxSession wxSession=wxSessionManager.getSession(wxMpXmlMessage.getFromUser());
            if("猜你妹".equals(wxMpXmlMessage.getContent())){
                if(wxSession.getAttribute("guessing")==null){
                    WxMpKefuMessage message=WxMpKefuMessage.TEXT()
                            .toUser(wxMpXmlMessage.getFromUser())
                            .content("请猜一个100以内的数字")
                            .build();
                    wxMpService.getKefuService().sendKefuMessage(message);
                }
                else {
                    WxMpKefuMessage message=WxMpKefuMessage.TEXT().toUser(wxMpXmlMessage.getFromUser())
                            .content("放弃了吗？那请重新猜一个100以内的数字").build();
                    wxMpService.getKefuService().sendKefuMessage(message);
                }

                wxSession.setAttribute("guessing", Boolean.TRUE);
                wxSession.setAttribute("number", this.random.nextInt(100));
            }

            boolean guessing = (Boolean) wxSession.getAttribute("guessing");
            if (guessing) {
                int answer = (Integer) wxSession.getAttribute("number");
                int guessNumber = Integer.valueOf(wxMpXmlMessage.getContent());
                if (guessNumber < answer) {
                    WxMpKefuMessage m = WxMpKefuMessage
                            .TEXT()
                            .toUser(wxMpXmlMessage.getFromUser())
                            .content("小了")
                            .build();
                    wxMpService.getKefuService().sendKefuMessage(m);

                } else if (guessNumber > answer) {
                    WxMpKefuMessage m = WxMpKefuMessage
                            .TEXT()
                            .toUser(wxMpXmlMessage.getFromUser())
                            .content("大了")
                            .build();
                    wxMpService.getKefuService().sendKefuMessage(m);
                } else {
                    WxMpKefuMessage m = WxMpKefuMessage
                            .TEXT()
                            .toUser(wxMpXmlMessage.getFromUser())
                            .content("Bingo!")
                            .build();
                    wxSession.removeAttribute("guessing");
                    wxMpService.getKefuService().sendKefuMessage(m);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

}
